package com.fishleap.config;

import com.fishleap.pojo.Configuration;
import com.fishleap.pojo.MapperStatement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

/**
 * @author zlg
 * @create 2020-04-21 23:38
 * 用来封装解析 *mapper.xml 文件的方法
 */
public class XmlMapperBuilder {

    private Configuration configuration;

    // 创建对象时初始化configuration对象
    public XmlMapperBuilder(Configuration configuration) {
        this.configuration = configuration;
    }

    // 解析 mapper文件
    public void parseMapper(InputStream inputStream) throws DocumentException {
        // 获取document对象
        Document document = new SAXReader().read(inputStream);
        Element rootElement = document.getRootElement();
        String namespace = rootElement.attributeValue("namespace");

        // 查找并遍历select节点
        List<Element> selectNodes= rootElement.selectNodes("//select");
        /*for (Element selectNode : selectNodes) {
            // 属性参数是字符串
            String id = selectNode.attributeValue("id");
            String paramterType = selectNode.attributeValue("paramterType");
            String resultType = selectNode.attributeValue("resultType");
            String sql = selectNode.getTextTrim();

            MapperStatement mapperStatement = new MapperStatement();
            mapperStatement.setId(id);
            mapperStatement.setParamterType(paramterType);
            mapperStatement.setResultType(resultType);
            mapperStatement.setSql(sql);

            String key = namespace + "." + id;
            configuration.getMapperStatementMap().put(key,mapperStatement);
        }*/

        // 增加insert / update / delete 节点解析
        List<Element> insertNodes = rootElement.selectNodes("//insert");
        List<Element> updateNodes = rootElement.selectNodes("//update");
        List<Element> deleteNodes = rootElement.selectNodes("//delete");

        // 调用方法进行节点解析并封装到configuration中
        traverseNodes(selectNodes, namespace);
        traverseNodes(insertNodes, namespace);
        traverseNodes(updateNodes, namespace);
        traverseNodes(deleteNodes, namespace);
    }

    // 解析节点的信息并封装到configuration.mapperStatementMap
    public void traverseNodes(List<Element> elements, String namespace) {
        for (Element node : elements) {
            // 属性参数是字符串
            String id = node.attributeValue("id");
            String paramterType = node.attributeValue("paramterType");
            String resultType = node.attributeValue("resultType");
            String sql = node.getTextTrim();

            MapperStatement mapperStatement = new MapperStatement();
            mapperStatement.setId(id);
            mapperStatement.setParamterType(paramterType);
            mapperStatement.setResultType(resultType);
            mapperStatement.setSql(sql);

            String key = namespace + "." + id;
            configuration.getMapperStatementMap().put(key, mapperStatement);
        }
    }

}
